﻿using System.Collections.ObjectModel;

using TimeSpan.Services;

namespace TimeSpan.Views.Pages;

public partial class NoteViewModel(WorkflowService workflow, IConfig<NotesConfig> notesConfig): ObservableObject {

    [ObservableProperty]
    private ObservableCollection<Note> _notes = notesConfig.Config.Notes;
    
    [ObservableProperty]
    private ObservableCollection<Note> _displayNotes = [.. notesConfig.Config.Notes];

    [ObservableProperty]
    private string _searchText = string.Empty;

    [RelayCommand]
    private void EditNote(Note note) {
        workflow.AddOrEditNote.OnNext(note);
        Search();
    }

    [RelayCommand]
    private void DeleteNote(Note note) {
        workflow.DeleteNote.OnNext(note);
        Search();
    }

    [RelayCommand]
    private void AddNote() {
        workflow.AddOrEditNote.OnNext(null);
        Search();
    }

    [RelayCommand]
    private void Search() {
        DisplayNotes = [.. Notes.Where(x => x.Title.Contains(SearchText, StringComparison.CurrentCultureIgnoreCase))];
    }

    [RelayCommand]
    private void Clear() {
        SearchText = string.Empty;
        Search();
    }

}